package com.jblv.frame.user.service.bll.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jblv.frame.common.enums.ExceptionEnum;
import com.jblv.frame.common.exception.FrameException;
import com.jblv.frame.common.type.BooleanType;
import com.jblv.frame.common.utils.UUIDGenerator;
import com.jblv.frame.user.api.bll.ISysLogBLL;
import com.jblv.frame.user.api.entity.SysLog;
import com.jblv.frame.user.api.service.ISysLogService;
import com.jblv.frame.user.api.service.ISysLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description 后台日志业务逻辑类
 * @Author    yangtao
 * @Create    2020-04-21
 **/
@Component
@Service
public class SysLogBLLImpl implements ISysLogBLL {

    @Autowired
    private ISysLogService sysLogService;

    private static Logger logger = LoggerFactory.getLogger(SysLogBLLImpl.class);


    @Override
    public String saveLog(String username, String operation, String method, String params, String response, String ip) {
        SysLog sysLog = new SysLog();
        sysLog.setUsername(username);
        sysLog.setOperation(operation);
        sysLog.setMethod(method);
        sysLog.setParams(params);
        sysLog.setResponse(response);
        sysLog.setIp(ip);

        return save("1",sysLog);
    }

    @Override
    public SysLog get(String id) {

//        String key = BaseCacheUtils.getBeanCacheKeyById(id);
        SysLog bean = null;//BaseCacheUtils.get(key);
        if (bean != null) {
            return bean;
        }
        EntityWrapper<SysLog> wrapper = new EntityWrapper<>();
        wrapper.eq("ID",id);
        bean = sysLogService.selectOne(wrapper);
        if (bean != null) {
            logger.info("add cache");
            //addCache(bean);
        }

        return bean;
    }


    @Override
    public String save(String accountId, SysLog sysLog) {
        if (sysLog == null) {
            return null;
        }
        if (StringUtils.isEmpty(sysLog.getId())) {
            sysLog.setId(UUIDGenerator.getUUID());
        }

        sysLog.setCreateTime(System.currentTimeMillis());

        boolean save = false;
        try {
            save = sysLogService.insert(sysLog);
        }catch (Exception e){
            logger.error("插入错误:{}",e.getMessage());
        }
        if (save) {
            //addCache(sysLog);
            return sysLog.getId();
        }

        return null;
    }

    @Override
    public boolean update(String operatorId, SysLog sysLog) {

        SysLog sysLogCheck = get(sysLog.getId());
        if (sysLogCheck != null) {
            throw new FrameException(ExceptionEnum.PARAM_ERROR);
        }
        if (sysLog != null) {

            boolean flag = sysLogService.updateById(sysLog);
            if (flag) {
                //SysLog newBean = sysLogService.selectById(sysLog.getId());
                //updateCache(oldUserInfo, newBean);
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean remove(String operatorId, String id) {

        if (StringUtils.isEmpty(id)) {
            return false;
        }
        SysLog sysLog = get(id);
        if (sysLog != null) {
            SysLog sysLogUpdate = new SysLog();
            sysLogUpdate.setId(id);
            Boolean flag = sysLogService.updateById(sysLogUpdate);
            if (flag) {
                //removeCache(bayonetInfo);
                return true;
            }
        }
        return false;
    }



}
